package com.hitalkie.teacher.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.hitalkie.teacher.R;
import com.hitalkie.teacher.model.TopicHint;

import java.util.ArrayList;
import java.util.List;


/**
 * Date 18/01/2017
 *
 * @author charliema
 */

public class LessonHintRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TopicHint> mData = new ArrayList<>();

    private OnItemClickListener mOnItemClick;

    public void setData(List<TopicHint> list) {
        if (list != null) {
            this.mData = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<TopicHint> list) {
        if (list != null) {
            this.mData.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClick) {
        mOnItemClick = onItemClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).bindData(mData.get(holder.getAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * view holder
     */
    private class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitleEn;
        View llDetail;
        TextView tvTitleZh;
        TextView tvKeyword;

        ViewHolder(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lesson_hint, parent, false));

            tvTitleEn = (TextView) itemView.findViewById(R.id.tvTitleEn);
            llDetail = itemView.findViewById(R.id.llDetail);
            tvTitleZh = (TextView) itemView.findViewById(R.id.tvTitleZh);
            tvKeyword = (TextView) itemView.findViewById(R.id.tvKeyword);
        }

        void bindData(final TopicHint data) {
            if (data == null) {
                return;
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClick != null) {
                        mOnItemClick.onItemClick(data);
                    }
                }
            });
//            Picasso.with(ivAvatar.getContext()).load(data.avatar)
//                    .placeholder(R.drawable.ic_default_avatar)
//                    .error(R.drawable.ic_default_avatar)
//                    .fit().into(ivAvatar);
            if (data.isExpanding) {
                itemView.setBackgroundColor(Color.parseColor("#ffffff"));
            } else {
                itemView.setBackground(null);
            }
            tvTitleEn.setText(data.title_en);
            tvTitleEn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean nextStatus = !data.isExpanding;
                    for (TopicHint topicHint : mData) {
                        topicHint.isExpanding = false;
                    }
                    data.isExpanding = nextStatus;
                    notifyDataSetChanged();
                }
            });

            llDetail.setVisibility(data.isExpanding ? View.VISIBLE : View.GONE);

            tvTitleZh.setText(data.title_zh);
            if (data.keywords != null && data.keywords.length() > 0) {
                tvKeyword.setVisibility(View.VISIBLE);
                tvKeyword.setText(data.keywords);
            } else {
                tvKeyword.setVisibility(View.GONE);
            }


        }
    }

    /**
     * click event
     */
    public interface OnItemClickListener {
        void onItemClick(TopicHint data);

        void onSample(TopicHint data);

        void onNote(TopicHint data);

    }

}
